package autopeople.service;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import autopeople.domain.auto.Auto;

@Transactional
@Named
// rune.forberg so I can use Inject on className instead of
// @Resource("autoService")
public class AutoServiceImpl implements AutoService {

	protected static Logger logger = Logger.getLogger("service");

	@Inject
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 * 
	 * @see autopeople.service.AutoService#getAll()
	 */
	@Override
	public List<Auto> getAll() {
		logger.debug("Retrieving all autos");

		// retrieve session from Hibernate
		final Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		@SuppressWarnings("unchecked")
		final
		List<Auto> list = session.createQuery("FROM Auto").list();

		// Retrieve all
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see autopeople.service.AutoService#add(autopeople.domain.auto.Auto)
	 */
	@Override
	public void add(Auto auto) {
		logger.debug("Adding a new auto");

		// Retrieve session from Hibernate
		final Session session = sessionFactory.getCurrentSession();
		session.save(auto);
	}
}
